﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Devtm.Generator.ModelStacks
{

    public interface IStack
    {
        object PopItem();    
    }

    public class CustomStack<T> : Stack<StackContainer<T>>, IStack
    {

        //private Stack<StackContainer<T, TContainer>> st = new Stack<StackContainer<T, TContainer>>();
        internal protected IStackItem container;

        public CustomStack(IStackItem writer)
        {
            this.container = writer;
        }


        protected virtual void Enter(object sender, object item)
        {
            IStackItem container = sender as IStackItem;
            container.Enter(item);
        }

        protected virtual void Exit(object sender, object item)
        {
            IStackItem container = sender as IStackItem;
            container.Exit(item);
            if (Count > 0)
                CurrentBox = Peek();
            else
                CurrentBox = null;
        }

        public StackContainer<T> AddBlock(T item)
        {
            var box = new StackContainer<T>(item, this, Exit);
            this.Push(box);
            CurrentBox = box;
            Enter(container, item);
            return box;
        }


        public StackContainer<T> CurrentBox { get; private set; }
        public T CurrentItem { get { return CurrentBox.Item; } }


        public object PopItem()
        {
            return this.Pop().Item;
        }


    }

}
